<?php
/** 续投奖励发放错误修复 **/
date_default_timezone_set('Asia/Shanghai');//时区配置
include 'functions.php';
$conn=mysql_connect("localhost","new_idc_gsdai","new_idc_gsdai#!.mysq1");
mysql_select_db("new_gsdai", $conn);
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);

$today = strtotime('2013-09-01');//1377964800

$topvip = array(//高级VIP名单 =>开始时间
	'947'=> strtotime('2013-07-01'),
	'1670'=> strtotime('2013-07-01'),
	'1762'=> strtotime('2013-07-01'),
	'2834'=> strtotime('2013-07-01'),
	'2857'=> strtotime('2013-07-01'),
	'2868'=> strtotime('2013-07-01'),
);

$sql = "select * from ych_account_log where ftype=40 and add_time>={$today}";
$result = db_fetch_arrays($sql, $conn);

$othertime = strtotime("2013-03-01 00:00:00");
$time2 = strtotime('2013-07-01');//千分之1.2回款奖励执行时间
foreach ($result as $value){
	//投标总额
	$mode = $value['user_id']%100; 
	$sql = "select sum(money) as toubiao from `ych_account_log_$mode` where user_id={$value['user_id']} and ftype=8 and add_time>={$othertime}";
	$row = db_fetch_array($sql,$conn);
	$toubiao = isset($row['toubiao'])?$row['toubiao'] : 0;
	
	//充值总额
	$sql = "SELECT SUM(money) AS chongzhi FROM `ych_account_recharge` WHERE user_id={$value['user_id']} AND `fstatus`=1 AND `ftype`>0 and add_time>={$othertime}";
	$row = db_fetch_array($sql,$conn);
	$chongzhi = isset($row['chongzhi'])?$row['chongzhi'] : 0;
	
	//提现收取的手续费
	$sql = "select sum(fee) as tixian from `ych_account_cash` where user_id={$value['user_id']} and fstatus='4' and add_time>={$othertime}";
	$row = db_fetch_array($sql,$conn);
	$tixian = isset($row['tixian'])?$row['tixian'] : 0;
	
	//奖励
	$tmpArr = array(11,39,41);
	$sql = "select sum(money) as jiangli from `ych_account_log_$mode` where user_id={$value['user_id']} and ftype in(".implode(',',$tmpArr).") and add_time>={$othertime}";
	$row = db_fetch_array($sql,$conn);
	$jiangli = isset($row['jiangli'])?$row['jiangli'] : 0;
	
	$huikuanjiangli = 0;
	
	if(isset($topvip[$value['user_id']])){//VIP用户
		$vipBegin = $topvip[$value['user_id']];//高级VIP执行时间
		//高级VIP执行前的回款奖励
		$sql = "select sum(money) as huikuanjiagli1 from `ych_account_log_$mode` where user_id={$value['user_id']} and ftype=40 and  `add_time`>={$othertime} and `add_time`<{$vipBegin}";
		$row = db_fetch_array($sql,$conn);
		$huikuanjiagli1 = isset($row['huikuanjiagli1'])?$row['huikuanjiagli1'] : 0;
		$huikuanjiagli1 = $huikuanjiagli1 * 1000;
		//高级VIP执行后的回款奖励
		$sql = "select sum(money) as huikuanjiagli2 from `ych_account_log_$mode` where user_id={$value['user_id']} and ftype=40 and  `add_time`>={$vipBegin}";
		$row = db_fetch_array($sql,$conn);
		$huikuanjiagli2 = isset($row['huikuanjiagli2'])?$row['huikuanjiagli2'] : 0;
		$huikuanjiagli2 = round($huikuanjiagli2 * 10000 / 15,0);
		$huikuanjiangli = $huikuanjiagli1 + $huikuanjiagli2;
		
	}else{//普通用户
		//$time2 = strtotime('2013-07-01');//千分之1.2回款奖励执行时间
		//千1奖励
		$sql = "select sum(money) as huikuanjiagli1 from `ych_account_log_$mode` where user_id={$value['user_id']} and ftype=40 and  `add_time`>={$othertime} and `add_time`<{$time2}";
		$row = db_fetch_array($sql,$conn);
		$huikuanjiagli1 = isset($row['huikuanjiagli1'])?$row['huikuanjiagli1'] : 0;
		$huikuanjiagli1 = $huikuanjiagli1 * 1000;
		//echo "发放千1回款奖励的金额:",$huikuanjiagli1,"<br>";
		//千分之1.2奖励
		$sql = "select sum(money) as huikuanjiagli2 from `ych_account_log_$mode` where user_id={$value['user_id']} and ftype=40 and  `add_time`>={$time2}";
		$row = db_fetch_array($sql,$conn);
		$huikuanjiagli2 = isset($row['huikuanjiagli2'])?$row['huikuanjiagli2'] : 0;
		$huikuanjiagli2 = round($huikuanjiagli2 * 10000 / 12,0);
		//echo "发放千1.2回款奖励金额:",$huikuanjiagli2,"<br>";
		$huikuanjiangli = $huikuanjiagli1 + $huikuanjiagli2;
	}
	
	$hk_toubiao = $toubiao - $chongzhi - $jiangli - $huikuanjiangli + $tixian;
	if($value['user_id']==1670){
		echo "投标：$toubiao，充值：$chongzhi，奖励：$jiangli，回款奖励：$huikuanjiangli，提现：$tixian";
	}
	//应该发放的奖励
	if(isset($topvip[$value['user_id']]) && time() > $topvip[$value['user_id']]){
		$hk_award = intval($hk_toubiao * 0.0015);
	}else{
		$hk_award = intval($hk_toubiao * 0.0012);
	}
	//多发出的奖励修复
	if($value['money']>$hk_award){
		//多发出的奖励
		$errMoney = $value['money']-$hk_award;
//		echo " ###应发：".$hk_award;
//		echo " ###已发：".$value['money'];
//		echo " ###多发：".$errMoney;
//		echo "<hr>";
		
		if($errMoney>$value['money']){
			$errMoney = $value['money'];
		}
		
		//修复account表  total、use_money
		$sql = "update ych_account set total=total-$errMoney,use_money=use_money-$errMoney where user_id=".$value['user_id'];
		mysql_query($sql,$conn);
		//修复主表日志
		$sql = "update ych_account_log set total=total-$errMoney,use_money=use_money-$errMoney where user_id=".$value['user_id']." and id>=".$value['id'];
		mysql_query($sql,$conn);
		$sql = "update ych_account_log set money=money-$errMoney where id=".$value['id'];
		mysql_query($sql,$conn);
		//修复分表日志
		$sql = "update ych_account_log_$mode set total=total-$errMoney,use_money=use_money-$errMoney where user_id=".$value['user_id']." and id>=".$value['id'];
		mysql_query($sql,$conn);
		$sql = "update ych_account_log_$mode set money=money-$errMoney where id=".$value['id'];
		mysql_query($sql,$conn);
	}
}

echo "end";
?>